import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import RegisterView from "@/views/RegisterView.vue";
import PersonInfoView from "@/views/PersonInfoView";
// import Permission from "@/views/Permission";
import WeiChatView from "@/views/admin/WeiChatView";
import IndexView from '@/views/IndexView';
import LoginView from "@/views/LoginView";
import Dept from "@/views/admin/Dept.vue";
import Job from "@/views/admin/Job.vue";
import Menu from "@/views/admin/Menu.vue";
import Role from "@/views/admin/Role.vue";
import User from "@/views/admin/User.vue";
import PatientRegister from "@/views/admin/PatientRegister.vue";
import PaymentPlatform from "@/views/admin/PaymentPlatform.vue";
import DoctorDiagnosis from "@/views/admin/DoctorDiagnosis.vue";
import DocView from "@/views/admin/DocView.vue";
import ToDoctorDiagnosis from "@/views/admin/ToDoctorDiagnosis.vue";
import ToDocView from "@/views/admin/ToDocView.vue";
import AbsenceRequest from "@/views/admin/AbsenceRequest.vue";
import LeaveManage from "@/views/admin/LeaveManage.vue";
import Hello from "@/components/Hello.vue";
import DrugAdministration from "@/views/admin/DrugAdministration.vue";
import PharmacyPrescription from "@/views/admin/PharmacyPrescription.vue";
import NewDrug from "@/views/admin/NewDrug.vue";

import ForgetPassword from "@/views/ForgetPassword";
import Reset_Password from "@/views/Reset_Password";
import HospitalInfoView from "@/views/HospitalInfoView";
import {setSessionStorage} from "@/common";
import ModifyInfo from "@/views/ModifyInfo";
import ModifyPwdView from "@/views/ModifyPwdView";

import InWard from "@/views/admin/InWard.vue";
import Ward from "@/views/admin/Ward.vue";
import NewRoom from "@/views/admin/NewRoom.vue";
import ToInWard from "@/views/admin/ToInWard.vue";
import AddrView from "@/views/admin/AddrView";

Vue.use(VueRouter)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [

    {
        path: '/hospitalinfo',
        name: 'hospinfo',
        component: HospitalInfoView
    },
    {
        path: '/modifypwd',
        name: 'modifypwd',
        component: ModifyPwdView
    },

    {
        path: '/login',
        name: 'login',
        component: LoginView
    },
    {
        path: '/register',
        name: 'register',
        component: RegisterView
    },
    {
        path: '/forget',
        name: 'forget',
        component: ForgetPassword
    },
    {
        path: '/reset',
        name: 'reset',
        component: Reset_Password
    },
    {
        path: '/index',
        name: 'index',
        component: IndexView,
        meta: {
            title: '首页'
        },
        children: [
            {
                path: '/modify',
                name: 'modify',
                component: ModifyInfo
            },
            {
                path: '/chat',
                name: 'chat',
                component: WeiChatView
            },
            {
                path: '/info',
                name: 'info',
                component: PersonInfoView
            },
            {
                path: '/index',
                name: 'Hello',
                component: Hello,
                meta: {
                    title: '首页'
                }
            },
            {
                path: '/index/dept',
                name: 'dept',
                component: Dept,
                meta: {
                    title: '部门管理'
                }
            },
            {
                path: '/index/user',
                name: 'user',
                component: User,
                meta: {
                    title: '用户管理'
                }
            },
            {
                path: '/index/role',
                name: 'role',
                component: Role,
                meta: {
                    title: '角色管理'
                }
            },
            {
                path: '/index/menu',
                name: 'menu',
                component: Menu,
                meta: {
                    title: '菜单管理'
                }
            },
            {
                path: '/index/job',
                name: 'job',
                component: Job,
                meta: {
                    title: '岗位管理'
                }
            },
            {
                path: '/index/PatientRegister',
                name: 'PatientRegister',
                component: PatientRegister,
                meta: {
                    title: '现场挂号'
                }
            },
            {
                path: '/index/PaymentPlatform',
                name: 'PaymentPlatform',
                component: PaymentPlatform,
                meta: {
                    title: '缴费平台'
                }
            },
            {
                path: '/index/doctorDiagnosis',
                name: 'DoctorDiagnosis',
                component: DoctorDiagnosis,
                meta: {
                    title: '医生诊断'
                }
            },
            {
                path: '/index/toDoctorDiagnosis',
                name: 'ToDoctorDiagnosis',
                component: ToDoctorDiagnosis,
                meta: {
                    title: '详细诊断'
                }
            },
            {
                path: '/index/docView',
                name: 'DocView',
                component: DocView,
                meta: {
                    title: '诊断数据'
                }
            },
            {
                path: '/index/toDocView',
                name: 'ToDocView',
                component: ToDocView,
                meta: {
                    title: '诊断结果'
                }
            },
            {
                path: '/index/drugAdministration',
                name: 'DrugAdministration',
                component: DrugAdministration,
                meta: {
                    title: '药品管理'
                }
            },
            {
                path: '/index/pharmacyPrescription',
                name: 'PharmacyPrescription',
                component: PharmacyPrescription,
                meta: {
                    title: '药房开单'
                }
            },
            {
                path: '/index/newDrug',
                name: 'NewDrug',
                component: NewDrug,
                meta: {
                    title: '新建药品'
                }
            },
            {
                path: '/index/inWard',
                name: 'inWard',
                component: InWard,
                meta: {
                    title: '住院信息'
                }
            },
            {
                path: '/index/toInWard',
                name: 'ToInWard',
                component: ToInWard,
                meta: {
                    title: '住院办理'
                }
            },
            {
                path: '/index/Ward',
                name: 'Ward',
                component: Ward,
                meta: {
                    title: '病房管理'
                }
            },
            {
                path: '/index/newRoom',
                name: 'NewRoom',
                component: NewRoom,
                meta: {
                    title: '新建病房'
                }
            },
            {
                path: '/index/absenceRequest',
                name: 'AbsenceRequest',
                component: AbsenceRequest,
                meta: {
                    title: '请假单'
                }
            },
            {
                path: '/index/leaveManage',
                name: 'LeaveManage',
                component: LeaveManage,
                meta: {
                    title: '假单管理'
                }
            },
            {
                path: '/index/addr',
                name: 'Addr',
                component: AddrView,
                meta: {
                    title: '医院地址'
                }
            },

        ]

    },

    {
        path: '/',
        name: 'home',
        component: HomeView
    },



]


const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


// 假设你有一个函数来检查用户是否已登录
function isLoggedIn() {
    // 这里根据你的实际情况返回用户登录状态
    // 例如，你可以从localStorage或vuex中获取登录状态
    // 假设登录状态保存在名为isUserLoggedIn的变量中
    const token = sessionStorage.getItem('token');
    console.log(token, '>>>11>>>>')

    return token;
}

// isLoggedIn()

//上面的代码都是项目自带的，我们需要写的代码就放在这个后面
router.beforeEach((to, from, next) => {
    //这里的to 就是要去哪里 相当于 this.$Router.push()
    //from表示从哪个页面来  next 表示下一步去执行什么操作
    //这里用道德token是后端提供的 我们在登录页登录的时候获取到token存放到本地缓存里
    // 或者存放在vuex中都可以看自己的需要，放在vuex中需要考虑页面刷新vuex中的数据消失的问题
    // const token = sessionStorage.getItem('token');
    // console.log(token)
    // const token = 1

    // path: '/login',
    //   name: 'Login',
    //   component:'../../src/login/login.vue' 这个是页面的路径
    //这个name就是路由中的name属性


    let ret = isLoggedIn()


    if (to.path == '/' || to.path == "/forget" || to.path == '/register' || to.path=='/reset') {//登陆页面，直接走
        if (ret) {
            next({path: '/index'})
        } else {
            next()
        }

        return false;
    } else if (!ret) {  //token不存在，跳转到登陆页面
        next({path: '/'})
        return false;
    } else if (ret) {
        console.log('ssssssssssssssss')
        next()
        return false;
    }
})


// const router = new VueRouter({
//   routes,
// });
//
// // 假设你有一个函数来检查用户是否已登录
// function isLoggedIn() {
//   // 这里根据你的实际情况返回用户登录状态
//   // 例如，你可以从localStorage或vuex中获取登录状态
//   // 假设登录状态保存在名为isUserLoggedIn的变量中
//   return isUserLoggedIn;
// }
//
// router.beforeEach((to, from, next) => {
//   // 检查用户是否已登录
//   if (to.name !== 'Login' && !isLoggedIn()) {
//     // 如果用户未登录且不是前往登录页面，重定向到登录页
//     next({ name: 'Login' });
//   } else if (to.name === 'Login' && isLoggedIn()) {
//     // 如果用户已登录且要前往登录页面，重定向到首页
//     next({ name: 'Home' });
//   } else {
//     // 其他情况直接通过
//     next();
//   }
// });


export default router
